package gamePlay;

import gui.BetMenu;
import gui.QuestionMenu;
import gui.ResultsMenu;
import gui.WinMenu;

import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.Scanner;

//The game class contains the running methods to actually run the game
//It creates a PlayerClass Obj to store player information and keep
//track of players and their stats

public class Game {
	
	// Random comment to see if committing works...
	
	Scanner keyboard = new Scanner(System.in); //Game set up variables
	
	String name;
	ArrayList<Player> players = new ArrayList<Player>(); 	//Array list of the players in the game
	ArrayList<Question> questions = new ArrayList<Question>();	// ArrayList of questions for the game
	int num_players;  //number of players in the game, for multiplayer set up?
	String score_type;
			/*how are we going to do scoring? how are we going to end the game?
			1) 7 turns, max score
			2) score limit*/
	Question current_question; //Current question
	int nextQuestion = 0; //Location in the question ArrayList
	ArrayList<Guess> guesses = new ArrayList<Guess>(); // ArrayList of the guesses for the current question
	ArrayList<Bet> bets = new ArrayList<Bet>(); // ArrayList of the bets for the current question
	public QuestionMenu questScreen;
    public BetMenu betScreen;
    public ResultsMenu resultScreen;
    public WinMenu winScreen;
    public javax.swing.Timer questTimer;
    public javax.swing.Timer betTimer;
    public javax.swing.Timer resultTimer;
    public javax.swing.Timer winTimer;

	private LayoutManager screenSize;
    
	/*
	 *  Default constructor
	 */
	public Game(){}
	
	/*
	 * Constructor
	 */
	public Game(String game_name, int players){
		name = game_name;
		num_players = players;
		for(int i = 0; i < 10; i++){
			questions.add(new Question());
		}
	}
	
	/*
	 * Getter for name variable
	 */
	public String getName(){
		return name;
	}
	
	/*
	 * Setter for name variable
	 */
	public void setName(String n){
		name = n;
	}
	
	/*
	 * Adds players to the game after checking if the game is full
	 */
	public boolean addPlayer(Player p){
		if(num_players > players.size()){
			players.add(p);
			return true;
		}
		else
			return false;
	}
	
	/*
	 * Updates the current question and displays it
	 * Currently does a print line
	 */
	public void displayQuestion(){
		current_question = questions.get(nextQuestion);	
		System.out.println(current_question.getText());
	}
	
	/*
	 * Returns the next question for the GUI
	 */
	public String getQuestionGUI(){
		current_question = questions.get(nextQuestion);
		return current_question.getText();
	}
	
	/*
	 * Displays the answer for the current question, currently as a print line
	 * Increments nextQuestion and updates current_question
	 */
	//public void displayAnswer(){
	//	System.out.println(current_question.getAnswer());
	//	nextQuestion++;
	//	current_question = questions.get(nextQuestion);
//	}
	
	/*
	 * Returns the answer for the current question.
	 * Increments nextQuestion and updates current_question
	 */
	public int getAnswer(){
		int answer = current_question.getAnswer();
		//nextQuestion++;
		//current_question = questions.get(nextQuestion);
		return answer;
	}
	
	public void updateQuestions(){
		nextQuestion++;
		current_question = questions.get(nextQuestion);
	}
	
	/*
	 * Used to add a guess to the guesses ArrayList
	 */
	public void receiveGuess(Guess guess){
		guesses.add(guess);
	}
	
	/*
	 * Used to add a bet to the bets ArrayList
	 */
	public void receiveBet(Bet bet){
		bets.add(bet);
	}
	
	/*
	 * Prints out the bets received
	 * Useful for testing/debugging.
	 */
	public void getBets(){
		for(int i = 0; i < bets.size(); i++){
			System.out.println(bets.get(i).getName() + "  " + bets.get(i).getChoice() + "   " + bets.get(i).getChipAmt());
		}
	}
	
	/*
	 * returns the list of bets
	 */
	public ArrayList<Bet> getBetList() {
		return bets;
	}
	
	public void clearGuesses(){
		guesses.clear();
	}
	
	/* 
	 * Calculates results from bets
	 * To be used after all bets are in	
	 */
	public void winnings(){
		int answer = current_question.getAnswer();
		Guess winner = guesses.get(0);
		int win_location = 0;
		// The for loop below determines which guess was closest to the answer
		for(int i = 0; i < guesses.size(); i++){
			int difference = Math.abs(answer - guesses.get(i).getGuess());
			if(difference < (Math.abs(answer - winner.getGuess()))){
				winner = guesses.get(i);
				win_location = i;
			}
		}
		// The for loop below gives chips to whoever bet on the winning guess
		// Currently it just gives them a 2x return on their bet
		// Doesn't currently remove chips for bets on wrong guesses
		for(int j = 0; j < bets.size(); j++){
			if(bets.get(j).getChoice() == win_location){
				String name = bets.get(j).getName();
				for(int z = 0; z < players.size(); z++){
					if(name.equals(players.get(z).getPlayer_name())){
						players.get(z).setGame_chips(bets.get(j).getChipAmt() + players.get(z).getGame_chips());
					}
				}
			}
		}
	}
	
	/*
	 * Prints out the guesses
	 * Useful for testing/debugging.
	 */
	public void getGuesses(){
		for(int i = 0; i < guesses.size(); i++){
			System.out.println(i + "  " + guesses.get(i).getName() + "     " + guesses.get(i).getGuess());
		}
	}
	
	public ArrayList<Integer> answers(){
		ArrayList<Integer> answer = new ArrayList<Integer>();
		for(int i = 0;i < guesses.size();i++){
			answer.add(guesses.get(i).getGuess());
		}
		return answer;
	}
	
	
	public Question getQuestion(){
		return current_question;
	}
	
	public ArrayList getQuestions(){
		return questions;
	}
	
	public ArrayList getPlayers(){
		return players;
	}
	
	
	public void createMenus(int screenSize){
		questScreen = new QuestionMenu(screenSize);
	    betScreen= new BetMenu(screenSize);
	    resultScreen= new ResultsMenu(screenSize);
	    winScreen = new WinMenu(screenSize);
	    questTimer = new javax.swing.Timer(1000, questScreen.new TimerListener());
	    betTimer = new javax.swing.Timer(1000, betScreen.new TimerListener());
	    resultTimer = new javax.swing.Timer(1000, resultScreen.new TimerListener());;
	    winTimer = new javax.swing.Timer(1000, winScreen.new TimerListener());
	}
	

}